// pages/address/address.js
const api = require("../../utils/api.js").default;
const myRequest = require("../../utils/request.js").default;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addList : [],
    isdeficon: '../../images/add02.png',
    notdeficon: '../../images/address.png',
  },

  // 设为默认
  isdef(e){
    const that = this;
    var isdef = e.currentTarget.dataset.isdef;
    var addid = e.currentTarget.dataset.id;
    isdef = !isdef;
    myRequest(api.address_save,"POST",{
      id:addid,
      is_default: isdef
    }, function(res){
      that.getuseraddres();
    })
  },

  // 查看收货地址详情
  getdetail(e){
    const that = this;
    var addid = e.currentTarget.dataset.id;
    myRequest(api.address_detail, "GET", { id: addid }, function (res) {
      console.log(res)
    });
  },

  // 编辑收货地址
  editaddress(e){
    const that = this;
    var addid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/addaddress/addaddress?id=' + addid,
    })
  },

  // 删除收货地址
  deladdress(e){
    const that = this;
    var addid = e.currentTarget.dataset.id;
    myRequest(api.address_delete, "POST", { id: addid },function(res){
      that.getuseraddres();
    });
  },


  // 跳转到 我的页面
  gouser() {
    wx.switchTab({
      url: '/pages/user/user',
    })
  },

  // 跳转到 增加地址页面
  goAddAddress(){
    wx.navigateTo({
      url: '/pages/addaddress/addaddress',
    })
  },

  // 获取用户收货地址列表
  getuseraddres(){
    const that = this;
    myRequest(api.address_list, 'GET', {}, function (res) {
      console.log(res.data.data);
      that.setData({
        addList: res.data.data,
      });
    })
    console.log(this.data.addList);
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getuseraddres();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getuseraddres();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  

})